#ifndef TC_MAEXPON_HPP
#define TC_MAEXPON_HPP

/** \file MAExpon.hpp
 * Definition of the MAExpon function template.
 *
 * (C) Copyright 2003-2006 by Semih Cemiloglu
 * All rights reserved, see COPYRIGHT file for details.
 *
 * $Id: MAExpon.hpp 110 2006-06-19 03:38:33Z semihc $
 *
 *
 */

#include "TA.hh"
#include "DBC.hpp"



namespace TC {

  //! Exponential moving average calculator.
  //! Based on percentage.
  TC_EXPORT template <typename InputIterator,
                      typename OutputIterator,
                      class Adaptor>
  void MAExponPct(InputIterator fromPos,
                  InputIterator fromEnd,
                  OutputIterator toPos,
                  const TAfp_t percent,
                  const Adaptor adaptor)
  {
    TC_PRE_CONDITION(percent > 0.0);
    TC_PRE_CONDITION(percent < 1.0);
    
    // Current value
    register TAfp_t val = adaptor(*fromPos);
    // Current average
    register TAfp_t avg = val;
    // Exp. mov. avg coefficients
    const TAfp_t k = percent;
    const TAfp_t l_k = 1.0 - k;


    while (fromPos != fromEnd) {
      // Get current value
      val = adaptor(*fromPos++);
      // Calculate the new average
      avg = val*k + avg*l_k;
      // Store it
      *toPos++ = avg;
    }
  }


  //! Exponential moving average calculator.
  //! Based on periods.
  TC_EXPORT template <typename InputIterator,
                      typename OutputIterator,
                      class Adaptor>
  void MAExpon(InputIterator fromPos,
               InputIterator fromEnd,
               OutputIterator toPos,
               const unsigned period,
               const Adaptor adaptor)
  {
    TC_PRE_CONDITION(period > 0);
    
    const TAfp_t percent = 2.0 / (period+1);
    MAExponPct(fromPos, fromEnd, toPos, percent, adaptor);
  }


} // end namespace


#endif /* Include guard */
